package com.ruoyi.myshop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.myshop.domain.Category;
import com.ruoyi.myshop.mapper.CategoryMapper;
import com.ruoyi.myshop.service.ICategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品分类Service业务层处理
 *
 * @author HuaQP
 * @date 2024-07-02
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Resource
    private CategoryMapper categoryMapper;

    private static final Logger log = LoggerFactory.getLogger(CategoryServiceImpl.class);

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询商品分类列表
     *
     * @param category 商品分类
     * @return 商品分类
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增商品分类
     *
     * @param category 商品分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        category.setCreateTime(DateUtils.getNowDate());
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改商品分类
     *
     * @param category 商品分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        category.setUpdateTime(DateUtils.getNowDate());
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }

    @Override
    public String importData(List<Category> list, boolean updateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Category category : list)
        {
            try
            {
                // 验证这条记录是否存在 By Id
                Category theCategory = categoryMapper.selectCategoryById(category.getId());
                if(ObjectUtil.isNull(theCategory)){ //不存在，则插入
                    int result = this.insertCategory(category);
                    if(result > 0){
                        successNum++;
                        successMsg.append("<br/>").append(successNum).append(" 分类Id: ").append(category.getId()).append(" 分类名称: ").append(category.getName()).append(" 导入成功");
                    }
                }else if(updateSupport){  // 存在，选项覆盖
                    int result = this.updateCategory(category);
                    if(result > 0){
                        successNum++;
                        successMsg.append("<br/>").append(successNum).append(" 分类Id: ").append(category.getId()).append(" 分类名称: ").append(category.getName()).append(" 导入成功");
                    }
                }
                else{ // 存在，选项不覆盖，跳过
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append(" 分类Id: ").append(category.getId()).append(" 分类名称: ").append(category.getName()).append(" 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + " 分类Id: " + category.getId() +  " 分类名称: " + category.getName() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
